/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

class BinarySearchInteger {
    public static void ComplexIntegerNumeric() {
        int count = 5000000;
        int[] testArray = new int[]{1, 2, 5, 7, 9, 12, 14, 23, 32, 43, 67, 77, 89, 90, 101, 122, 125, 142, 153}; // GenerateSearchArrayInteger();
        double start = System.nanoTime();
        int res = testArray[2];
        int testArrayLength = testArray.length;
        for (int i = 1; i < count; i++) {
            int value = testArray[i % res & (testArrayLength - 1)];
            int low = 0;
            int tmp = 0;
            int high = testArrayLength - 1; // 1023
            int middle = high >>> 1;
            for (; low <= high; middle = (low + high) >>> 1) {
                int test = testArray[middle];
                if (test > value) {
                    high = middle - 1;
                } else if (test < value) {
                    low = middle + 1;
                } else {
                    tmp = middle;
                    break;
                }
            }
            res += tmp;
        }
        double end = System.nanoTime();
        System.out.println("" + res);
        double time = (end - start) / 1000000;
        System.out.println("Numerical Calculation - ComplexIntegerNumeric:\t"+time+"\tms");
    }
}

class Benchmark {
    public static void main(String[] args) {
        BinarySearchInteger.ComplexIntegerNumeric();
    }
}
